home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / ed / ed.c < prev    next >
C/C++ Source or Header  |  1993-07-25  |  3KB  |  127 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <setjmp.h>
  4.  
  5. #define far
  6.  
  7. #include "ed.h"
  8. #include "paging.h"
  9. #include "unassmbl.h"
  10. #include "debug.h"
  11.  
  12. void movedata(int srcseg, int srcofs, int destseg, int destofs, int length);
  13.  
  14. ExternalDebuggerInfo edi;
  15. TSS a_tss;
  16. AREAS areas[MAX_AREA];
  17.  
  18. static int my_ds;
  19. static int app_ds;
  20. static int edi_seg;
  21. static int edi_ofs;
  22.  
  23. void edi_init()
  24. {
  25.   int i;
  26.   asm("movw $0xfe01,%ax");
  27.   asm("int $0x21");
  28.   asm("movl %edx,_edi_seg");
  29.   asm("movl %eax,_edi_ofs");
  30.   asm("xor  %eax,%eax");
  31.   asm("movw %ds,%ax");
  32.   asm("movl %eax,_my_ds");
  33.   movedata(edi_seg, edi_ofs, my_ds, (int)(&edi), sizeof(edi));
  34.   movedata(edi.a_tss_seg, edi.a_tss_ofs, my_ds, (int)(&a_tss), sizeof(TSS));
  35.   app_ds = a_tss.tss_ds;
  36.   movedata(edi.areas_seg, edi.areas_ofs, my_ds, (int)areas, sizeof(areas));
  37.   for (i=0; i<MAX_AREA; i++)
  38.   {
  39.     areas[i].first_addr -= edi.app_base;
  40.     areas[i].last_addr -= edi.app_base;
  41.   }
  42. }
  43.  
  44. void run_child(void)
  45. {
  46.   int i;
  47.   movedata(my_ds, (int)(&edi), edi_seg, edi_ofs, sizeof(edi));
  48.   movedata(my_ds, (int)(&a_tss), edi.a_tss_seg, edi.a_tss_ofs, sizeof(TSS));
  49.   asm("movw $0xfe00,%ax");
  50.   asm("int $0x21");
  51.   movedata(edi_seg, edi_ofs, my_ds, (int)(&edi), sizeof(edi));
  52.   movedata(edi.a_tss_seg, edi.a_tss_ofs, my_ds, (int)(&a_tss), sizeof(TSS));
  53.  
  54.   movedata(edi.areas_seg, edi.areas_ofs, my_ds, (int)areas, sizeof(areas));
  55.   for (i=0; i<MAX_AREA; i++)
  56.   {
  57.     areas[i].first_addr -= edi.app_base;
  58.     areas[i].last_addr -= edi.app_base;
  59.   }
  60. }
  61.  
  62. static int invalid_addr(word32 a, unsigned len)
  63. {
  64.   int i;
  65.   if ((int)invalid_addr > 0)
  66.     return 0;
  67.   for (i=0; i<MAX_AREA; i++)
  68.     if (a>=areas[i].first_addr && (a+len-1) <= areas[i].last_addr)
  69.       return 0;
  70.   printf("Invalid access to child, address %#x length %#x\n", a, len);
  71.   if (can_longjmp)
  72.     longjmp(debugger_jmpbuf, 1);
  73.   return 1;
  74. }
  75.  
  76. int read_child(word32 child_addr, void *buf, unsigned len)
  77. {
  78.   if (invalid_addr(child_addr, len))
  79.     return 1;
  80.   movedata(app_ds, child_addr, my_ds, (int)buf, len);
  81.   return 0;
  82. }
  83.  
  84. int write_child(word32 child_addr, void *buf, unsigned len)
  85. {
  86.   if (invalid_addr(child_addr, len))
  87.     return 1;
  88.   movedata(my_ds, (int)buf, app_ds, child_addr, len);
  89.   return 0;
  90. }
  91.  
  92. void ansi(int fg)
  93. {
  94.   if (!edi.ansi_mode)
  95.     return;
  96.   printf("\033[%d;%dm", (fg & A_bold) ? 1 : 0, 30+(fg&7));
  97. }
  98.  
  99. int main()
  100. {
  101.   char *fn;
  102.   int i, v;
  103.  
  104.   edi_init();
  105.  
  106.   fn = (char *)malloc(edi.filename_len + 1);
  107.   movedata(edi.filename_seg, edi.filename_ofs, my_ds, (int)(fn), edi.filename_len+1);
  108. /*
  109.   printf("text: %#08x - %#08x\n", areas[A_text].first_addr, areas[A_text].last_addr);
  110.   printf("data: %#08x - %#08x\n", areas[A_data].first_addr, areas[A_data].last_addr);
  111.   printf("bss:  %#08x - %#08x\n", areas[A_bss].first_addr, areas[A_bss].last_addr);
  112.   */
  113.   syms_init(fn);
  114.  
  115.   debugger();
  116.  
  117.   return 0;
  118. }
  119.  
  120. void *sbrk(int l)
  121. {
  122.   extern int end;
  123.   static int sold = (int)&end;
  124.   sold += l;
  125.   return (void *)(sold-l);
  126. }
  127.